package com.stefan.service.impl;

import com.stefan.entity.Product;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

@Component
public class ProductAggregateOperationImpl {
    @Autowired
    private MongoTemplate mongoTemplate;
    // 这里可以添加聚合操作的方法
    public void getSortedProducts() {
        // System.out.println("Sorted Products: " + sortedProducts);
        SortOperation id = Aggregation.sort(Sort.by(Sort.Direction.DESC,"_id"));
        ProjectionOperation project = project("_id", "product_name", "subscription_price", "currency");
        TypedAggregation<Product> typedAggregation = Aggregation.newAggregation(Product.class,id,project);
        AggregationResults<Product> aggregate = mongoTemplate.aggregate(typedAggregation, Product.class);
        List<Product> mappedResults = aggregate.getMappedResults();
        for (Product mappedResult : mappedResults) {
            System.out.println(mappedResult);
        }
    }
}
